package cn.wolfcode.shop.server.service.impl;

import cn.wolfcode.shop.api.domain.Catalog;
import cn.wolfcode.shop.api.service.ICatalogService;
import cn.wolfcode.shop.server.mapper.CatalogMapper;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class CatalogServiceImpl implements ICatalogService {

    @Autowired
    private CatalogMapper catalogMapper;

    @Override
    public List<Catalog> selecRootTree() {
        List<Catalog> catalogs = catalogMapper.selectRootTree();
        return catalogs;
    }

    @Override
    public List<Catalog> selectSonTree(Long parentId) {
        List<Catalog> catalogs = catalogMapper.selectSonTree(parentId);
        if (catalogs.size() == 0) {
            throw new RuntimeException("输入的ID不存在子分类");
        }
        return catalogs;
    }

    @Override
    public Catalog get(Long id) {
        Catalog catalog = catalogMapper.get(id);
        return catalog;
    }

    @Override
    public void update(Catalog catalog) {
        int update = catalogMapper.update(catalog);
        if (update == 0) {
            throw new RuntimeException("乐观锁异常...");
        }
    }

    @Override
    public List<?> selectAll() {
        return catalogMapper.list();
    }

    @Override
    public List<Catalog> listCatalogs() {return catalogMapper.listCatalogs();}

    @Override
    public void save(Catalog catalog) {
        catalogMapper.save(catalog);
    }

    @Override
    public void delete(Long id) {
        catalogMapper.delete(id);
    }

    @Override
    public List<Catalog> list() {
        return catalogMapper.list();
    }
}
